﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class OUTTIME : Form
    {
        static string connection = ConfigurationManager.AppSettings["ConStr"].ToString();
        SqlConnection con2 = new SqlConnection(connection);

        Fitness_club.helper get = new Fitness_club.helper();

        

        string str = string.Empty;

        public OUTTIME()
        {
            InitializeComponent();
        }

        private void OUTTIME_Load(object sender, EventArgs e)
        {

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMemberID.Text.Length == 0)
                {
                    MessageBox.Show("Member ID should not be empty !!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMemberID.Focus();
                    //this.ActiveControl = txtMemberID.Text;
                    return;
                }

                DateTime todayDate = DateTime.Now;
                string currenDate = todayDate.ToString("d");

                DateTime todayTime = DateTime.Now;
                string currenTime = todayTime.ToString("hh:mm:ss");

                DateTime ValidateDate = DateTime.Now;
                string CheckINDate = ValidateDate.Date.ToString("d");


                SqlCommand cmd1 = new SqlCommand("select COUNT(*)FROM Attendance WHERE MemberId = @memberid and indate = @indate and outdate IS NULL");
                cmd1.Connection = con2;
                cmd1.Parameters.Add("@memberid", SqlDbType.BigInt).Value = txtMemberID.Text;
                cmd1.Parameters.Add("@indate", SqlDbType.Date).Value = CheckINDate;
                con2.Open();
                int OBJ = Convert.ToInt32(cmd1.ExecuteScalar());

                if (OBJ > 0)// && (string.Equals(currenDate, ValidateDate)))
                {
                    
                    str = "update Attendance set outdate = @outdate, outtime = @outtime where MemberId = @memberid and indate = @indate AND outdate IS NULL";
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = str;
                    cmd.Parameters.Add("@memberid", SqlDbType.BigInt).Value = txtMemberID.Text;
                    cmd.Parameters.Add("@outdate", SqlDbType.Date).Value = currenDate;
                    cmd.Parameters.Add("@outtime", SqlDbType.Time).Value = currenTime;
                    cmd.Parameters.Add("@indate", SqlDbType.Date).Value = CheckINDate;
                    get.SqlCmd_Exec(cmd);

                    string message = "Out-Time Saved Successfully !!!";
                    string title = "GYM Exit/Out Time";
                    MessageBox.Show(message, title);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Invalid member id !!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                }
                con2.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("Invalid member id !!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            finally
            {
                AttendanceLanding landing = new AttendanceLanding();
                landing.Show();
                this.Dispose();
            }
        }

        private void txtMemberID_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        //private void buttonOUTTIME_Click(object sender, EventArgs e)
        //{            
        //    //txtOUTTIME.Text = DateTime.Now.ToString();//"h:mm:ss tt");
        //    txtOUTTIME.Text = DateTime.Now.ToString("h:mm:ss");
        //}
    }
}
